﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SCSEngine.ScreenManagement.Implement;
using SCSEngine.ScreenManagement;
using SCSEngine.Sprite;
using SCSEngine.Services;
using SCSEngine.ResourceManagement;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace SVLvsXNA.TriUanGame.GameScreen
{
    public class LanguageOption : BaseGameScreen
    {
        private static readonly Vector2 ButtonPosition = new Vector2(250f, 150f);
        private static readonly Vector2 ButtonSize = new Vector2(250f, 100f);

        private ISprite viet;
        private ISprite eng;

        private SCSEngine.Control.Button button;

        private Texture2D background;
        private SpriteBatch sprBatch;

        public LanguageOption(IGameScreenManager gsm)
            : base(gsm)
        {
            IResourceManager resMan = this.GamePage.Services.GetService(typeof(IResourceManager)) as IResourceManager;

            this.viet = resMan.GetResource<ISprite>("VietnameseButton");
            this.eng = resMan.GetResource<ISprite>("EnglishButton");
            button = new SCSEngine.Control.Button(this.GamePage, ButtonSprite);
            button.Position = ButtonPosition;
            button.Size = ButtonSize;
            button.OnTouched += this.OnButtonTouched;
            this.Components.Add(button);

            this.background = resMan.GetResource<Texture2D>("MainBackground");
            this.sprBatch = (this.GamePage.Services.GetService(typeof(SCSServices)) as SCSServices).SpriteBatch;
        }

        private ISprite ButtonSprite
        {
            get
            {
                if (Language.Prep == Language.VI)
                    return viet;

                return eng;
            }
        }

        private void OnButtonTouched(SCSEngine.Control.Button sender, EventArgs args)
        {
            if (Language.Prep == Language.VI)
                Language.Prep = Language.EN;
            else
            {
                Language.Prep = Language.VI;
            }

            button.Background = ButtonSprite;
        }

        public override void Update(GameTime gameTime)
        {
            if (this.GamePage.IsBackKeyPress)
            {
                Manager.RemoveCurrent();

                this.GamePage.IsBackKeyPress = false;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            this.sprBatch.Draw(this.background, Vector2.Zero, Microsoft.Xna.Framework.Color.White);

            base.Draw(gameTime);
        }
    }

    public class OptionScreenFactory : IGameScreenFactory
    {
        IGameScreenManager manager;
        public OptionScreenFactory(IGameScreenManager manager)
        {
            this.manager = manager;
        }

        // Singleton
        public IGameScreen CreateGameScreen()
        {
            return new LanguageOption(this.manager);
        }
    }
}
